const { get, run, all } = require('../config/db');

class ProtocolElement {
  // 创建协议元素
  static async create(protocolId, name, enName, elementType, position) {
    try {
      // 插入基本元素信息
      const result = await run(
        'INSERT INTO protocol_elements (protocol_id, name, en_name, element_type, position) VALUES (?, ?, ?, ?, ?)',
        [protocolId, name, enName, elementType, position]
      );
      
      const elementId = result.id;
      
      return { id: elementId, protocolId, name, enName, elementType, position };
    } catch (error) {
      console.error('创建协议元素失败:', error);
      throw error;
    }
  }
  
  // 添加固定类型元素属性
  static async addFixedProperties(elementId, value) {
    try {
      await run(
        'INSERT INTO fixed_element_properties (element_id, value) VALUES (?, ?)',
        [elementId, value]
      );
      
      return { elementId, value };
    } catch (error) {
      console.error('添加固定类型元素属性失败:', error);
      throw error;
    }
  }
  
  // 添加常规类型元素属性
  static async addNormalProperties(elementId, dataType) {
    try {
      await run(
        'INSERT INTO normal_element_properties (element_id, data_type) VALUES (?, ?)',
        [elementId, dataType]
      );
      
      return { elementId, dataType };
    } catch (error) {
      console.error('添加常规类型元素属性失败:', error);
      throw error;
    }
  }
  
  // 添加自动计算类型元素属性
  static async addAutoProperties(elementId, calcType) {
    try {
      await run(
        'INSERT INTO auto_element_properties (element_id, calc_type) VALUES (?, ?)',
        [elementId, calcType]
      );
      
      return { elementId, calcType };
    } catch (error) {
      console.error('添加自动计算类型元素属性失败:', error);
      throw error;
    }
  }
  
  // 更新元素位置
  static async updatePosition(elementId, position) {
    try {
      await run(
        'UPDATE protocol_elements SET position = ? WHERE id = ?',
        [position, elementId]
      );
      
      return { elementId, position };
    } catch (error) {
      console.error('更新元素位置失败:', error);
      throw error;
    }
  }
  
  // 更新元素基本信息
  static async update(elementId, name, enName) {
    try {
      await run(
        'UPDATE protocol_elements SET name = ?, en_name = ? WHERE id = ?',
        [name, enName, elementId]
      );
      
      return { elementId, name, enName };
    } catch (error) {
      console.error('更新元素信息失败:', error);
      throw error;
    }
  }
  
  // 更新固定类型元素属性
  static async updateFixedProperties(elementId, value) {
    try {
      await run(
        'UPDATE fixed_element_properties SET value = ? WHERE element_id = ?',
        [value, elementId]
      );
      
      return { elementId, value };
    } catch (error) {
      console.error('更新固定类型元素属性失败:', error);
      throw error;
    }
  }
  
  // 更新常规类型元素属性
  static async updateNormalProperties(elementId, dataType) {
    try {
      await run(
        'UPDATE normal_element_properties SET data_type = ? WHERE element_id = ?',
        [dataType, elementId]
      );
      
      return { elementId, dataType };
    } catch (error) {
      console.error('更新常规类型元素属性失败:', error);
      throw error;
    }
  }
  
  // 更新自动计算类型元素属性
  static async updateAutoProperties(elementId, calcType) {
    try {
      await run(
        'UPDATE auto_element_properties SET calc_type = ? WHERE element_id = ?',
        [calcType, elementId]
      );
      
      return { elementId, calcType };
    } catch (error) {
      console.error('更新自动计算类型元素属性失败:', error);
      throw error;
    }
  }
  
  // 删除元素
  static async delete(elementId) {
    try {
      await run(
        'DELETE FROM protocol_elements WHERE id = ?',
        [elementId]
      );
      
      return true;
    } catch (error) {
      console.error('删除元素失败:', error);
      throw error;
    }
  }
  
  // 获取元素库
  static async getElementLibrary(userId) {
    try {
      const rows = await all(
        `SELECT * FROM element_library 
         WHERE is_system = 1 OR user_id = ? 
         ORDER BY category, name`,
        [userId]
      );
      
      // 获取元素库属性
      const elements = [];
      for (const element of rows) {
        const properties = await all(
          'SELECT property_name, property_value FROM element_library_properties WHERE library_id = ?',
          [element.id]
        );
        
        const props = {};
        properties.forEach(prop => {
          props[prop.property_name] = prop.property_value;
        });
        
        elements.push({
          ...element,
          properties: props
        });
      }
      
      return elements;
    } catch (error) {
      console.error('获取元素库失败:', error);
      throw error;
    }
  }
}

module.exports = ProtocolElement;
